package com.nanbei.eduUcenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nanbei.commonUntil.JwtUtils;
import com.nanbei.commonUntil.MD5;
import com.nanbei.eduUcenter.entity.UcenterMember;
import com.nanbei.eduUcenter.entity.vo.LoginCenterMember;
import com.nanbei.eduUcenter.entity.vo.RegistCenterMember;
import com.nanbei.eduUcenter.mapper.UcenterMemberMapper;
import com.nanbei.eduUcenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanbei.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-01-19
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 登录
     * @param loginCenterMember
     * @return
     */
    @Override
    public String loginCenter(LoginCenterMember loginCenterMember) {

        //判断是否含有手机号和验证码
        if (StringUtils.isEmpty(loginCenterMember.getMobile()) || StringUtils.isEmpty(loginCenterMember.getPassword())){
            throw new GuliException(20001,"账号或密码为空");
        }

        //通过手机号查询个人信息
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile",loginCenterMember.getMobile());
        UcenterMember ucenterMember = this.baseMapper.selectOne(ucenterMemberQueryWrapper);

        //
        if (ucenterMember == null){
            throw new GuliException(20001,"该账号尚未注册");
        }

        //判断密码是否正确
        if (!MD5.encrypt(loginCenterMember.getPassword()).equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"密码错误");
        }

        if (ucenterMember.getIsDisabled()){
            throw new GuliException(20001,"该账号已被禁用");
        }

        String token = JwtUtils.getJwtToken(ucenterMember.getId(),ucenterMember.getNickname());

        return token;
    }

    /**
     * 注册账号
     * @param registCenterMember
     */
    @Override
    public void registerUser(RegistCenterMember registCenterMember) {

        String code = registCenterMember.getCode();   //验证码
        String mobile = registCenterMember.getMobile();  //手机号
        String nickname = registCenterMember.getNickname(); //昵称
        String password = registCenterMember.getPassword();  // 密码

        //判断是否有没填写信息
        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password))  {
            throw  new GuliException(20001,"有未填写信息");
        }

        //判断该邮箱是否注册
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile",mobile);
        Integer integer = this.baseMapper.selectCount(ucenterMemberQueryWrapper);
        if (integer>0){
            throw  new GuliException(20001,"该邮箱已被注册");
        }

        //判断验证码是否正确
        String a = (String) redisTemplate.opsForValue().get(mobile);
        if (!code.equals(a)){
            throw  new GuliException(20001,"验证码错误");
        }


        //对账号进行储存
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setNickname(nickname);
        ucenterMember.setAvatar("https://gulixg.oss-cn-shanghai.aliyuncs.com/2021/12/31/1b3cf61f-3810-4023-80b9-da8859bf6167file.png");
        ucenterMember.setIsDisabled(false);
        this.baseMapper.insert(ucenterMember);
    }


    /**
     * 根据日期统计一天的注册人数
     * @param day
     * @return
     */
    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }


}
